package com.idanchuang.component.businesslog.es.index;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.Objects;

/**
 * The ActionsPolicy
 *
 * @since 2020/7/17
 */
public class ActionsPolicy implements ToXContentObject {

    private final String name;
    private final ActionRole action;

    public ActionsPolicy(String name, ActionRole action) {
        this.name = name;
        this.action = action;
    }

    public String getName() {
        return name;
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject();
        builder.field(name, action);
        builder.endObject();
        return builder;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, action);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        ActionsPolicy other = (ActionsPolicy) obj;
        return Objects.equals(name, other.name) &&
                Objects.equals(action, other.action);
    }

    @Override
    public String toString() {
        return Strings.toString(this);
    }
}
